package cn.wkj.mall.common.web;

import cn.wkj.mall.common.ex.ServiceCode;
import cn.wkj.mall.common.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer state;
    private String message;
    private T data;

    public JsonResult() {
    }

    public JsonResult(Integer state, String message, T data) {
        this.state = state;
        this.message = message;
        this.data = data;
    }

    public static <T> JsonResult<T> ok( T data){
        return new JsonResult(ServiceCode.OK.getValue(),null,data);
    }
    public static JsonResult<Void> ok(){
        return JsonResult.ok(null);
    }

    public static JsonResult<Void> fail(Integer state, String message){
        return new JsonResult<>(state,message,null);
    }
    public static JsonResult<Void> fail(ServiceException e){
        return JsonResult.fail(e.getServiceCode().getValue(),e.getMessage());
    }
}
